library("gtools")
source("~/Desktop/Research/R/support_test/hawkes_utilities.R")
data_dir <- "~/Desktop/Research/lobster/"
save_dir <- "~/Disk/Research/image_support_test/"
dir.create("~/Disk/Research/image_support_test/data_agg_nosize/",showWarnings = FALSE)
dir.create("~/Disk/Research/image_support_test/data_agg_withsize/",showWarnings = FALSE)
dir.create("~/Disk/Research/image_support_test/plot_agg_nosize/",showWarnings = FALSE)
dir.create("~/Disk/Research/image_support_test/plot_agg_withsize/",showWarnings = FALSE)


model_config_list <- c("state",                                 #1
                       "time_customizedtwo",                    #2
                       "state_time_customizedtwo",              #3
                       "hawkes",                                #4
                       "state_hawkes",                          #5
                       "hawkes_time_customizedtwo",             #6
                       "state_hawkes_time_customizedtwo",       #7
                       "hawkes_LASSO",                          #8
                       "state_hawkes_time_customizedtwo_LASSO", #9
                       "hawkes_LASSO_0005",                     #10
                       "state_hawkes_time_customizedtwo_LASSO_0005") #11      






args <- commandArgs(trailingOnly=TRUE)
if(length(args) == 0){
  args_size <- 2
  model_config <- model_config_list[1]
}else{
  args_size <- as.numeric(args[1])
  model_config <-model_config_list[as.numeric(args[2])]
}

# "nosize" if arg is a even number; withsize" if arg odd number
if(args_size %% 2 == 0){
  config_size <- "nosize"
}else{
  config_size <- "withsize"
}

print(config_size)
print(model_config)

### Set global parameters
support_seq = c(20)
delta = 0.25


######## time factor labels for model_config: state_hawkes_timecustomizedtwo ################
time_1 = seq(ISOdatetime(2019,1,2,9,31,0), ISOdatetime(2019,1,2,10,0,0), by=60)
time_2 = seq(ISOdatetime(2019,1,2,10,1,0), ISOdatetime(2019,1,2,15,30,0), by=60*5)
time_3 = seq(ISOdatetime(2019,1,2,15,31,0), ISOdatetime(2019,1,2,16,0,0), by=60)
time_label = c(time_1, time_2, time_3)
time_label = unlist(lapply(time_label, function(x){substr(as.character(x), 12,16)}))


all_files = c( "AAPL_2019-01-02","AAPL_2019-01-03","AAPL_2019-01-04","AAPL_2019-01-07","AAPL_2019-01-08",  
               "AAPL_2019-01-10", "AAPL_2019-01-11","AAPL_2019-01-14","AAPL_2019-01-15","AAPL_2019-01-16", 
               "AAPL_2019-01-17", "AAPL_2019-01-18","AAPL_2019-01-22","AAPL_2019-01-23","AAPL_2019-01-24", 
               "AAPL_2019-01-25", "AAPL_2019-01-28","AAPL_2019-01-29","AAPL_2019-01-30","AAPL_2019-01-31")



for (support_max in support_seq){
  print(support_max)
  
  data_processed_dir <- paste0(save_dir, "/data_processed_", config_size, "/")
  out_dir <- paste0(save_dir, "/data_calculated_", config_size, "/")
  
  all_bigdt_files <- list.files(data_processed_dir)
  all_bigdt_files <- all_bigdt_files[grep("rds", all_bigdt_files)]
  all_calc_files <- list.files(out_dir)
  all_calc_files <- all_calc_files[grep(paste0("support",toString(support_max), "delta",toString(delta)), all_calc_files)]
  all_calc_files <- all_calc_files[grep(paste0( "\\hawkes_kernel_single.", model_config, ".\\b"), all_calc_files)]
  
  if(length(all_bigdt_files)==0){
    ticker_day_bigdt_files = c( "AAPL_2019-01-02","AAPL_2019-01-03","AAPL_2019-01-04","AAPL_2019-01-07","AAPL_2019-01-08",  
                   "AAPL_2019-01-10", "AAPL_2019-01-11","AAPL_2019-01-14","AAPL_2019-01-15","AAPL_2019-01-16", 
                   "AAPL_2019-01-17", "AAPL_2019-01-18","AAPL_2019-01-22","AAPL_2019-01-23","AAPL_2019-01-24", 
                   "AAPL_2019-01-25", "AAPL_2019-01-28","AAPL_2019-01-29","AAPL_2019-01-30","AAPL_2019-01-31")
  }else{
    ticker_day_bigdt_files <- sort(unique(sapply(strsplit(all_bigdt_files, "[.]"), function(x) x[1])))
  }
  for(each_ticker_day in ticker_day_bigdt_files){
    ticker_day_calc_files <- all_calc_files[grep(each_ticker_day, all_calc_files)]
    if(length(ticker_day_calc_files) != 24){
      cat(each_ticker_day, "not have all calc files\n",toString(support_max))
      next
    }
    hawkes_kernel <- lapply(ticker_day_calc_files, function(rds_file_name){
      readRDS(paste0(out_dir, rds_file_name))[[1]]
    })
    names(hawkes_kernel) <- sapply(strsplit(ticker_day_calc_files, "[.]"), function(x) substring(x[4],1,5))
    cols_all <- paste0(rep(sprintf("%+d", c(1:3, -(1:3))), each=3), "(", c("+", "-", "t"), ")")
    cols_all <- c(cols_all, c("p+(+)", "p+(-)", "p+(t)", "p-(+)", "p-(-)", "p-(t)"))
    hawkes_kernel <- hawkes_kernel[cols_all]
    
    if (model_config == "state" || model_config == "time_customizedtwo" || model_config == "state_time_customizedtwo"  ){
      mu_state <- lapply(hawkes_kernel, function(x) x[!grepl("design_matrix", names(x))])
      mu_state_names <- mixedsort(unique(unlist(lapply(mu_state, names))))
      mu_state_mat <- matrix(NA, nrow=length(mu_state_names), ncol=length(hawkes_kernel),
                             dimnames = list(mu_state_names, names(hawkes_kernel)))
      for(k in 1:length(mu_state)){
        mu_state_mat[names(mu_state[[k]]),k] <- mu_state[[k]]
      }
      hawkes_kernel_est <- mu_state_mat / delta
    }else{
      hawkes_kernel_est <- organize_kernel_output(hawkes_kernel)
      hawkes_kernel_est <- hawkes_kernel_est / delta
    }
    
    hawkes <- list(
      hawkes_kernel_est = hawkes_kernel_est,
      delta = delta,
      support_max = support_max
    )
    
    # for residuals  
    residuals <- lapply(ticker_day_calc_files, function(rds_file_name){
      readRDS(paste0(out_dir, rds_file_name))[[2]]
    })
    names(residuals) <- sapply(strsplit(ticker_day_calc_files, "[.]"), function(x) substring(x[4],1,5))
    residuals <- residuals[cols_all]
    
    
    
    support_string = paste0("_support",toString(support_max), "delta",toString(delta))
    
    if (config_size == "nosize"){
      saveRDS(hawkes, paste0(save_dir, "data_agg_nosize/hawkes_markov_pref_price_", each_ticker_day, support_string,"_",model_config,".rds"))
      saveRDS(residuals, paste0(save_dir, "data_agg_nosize/hawkes_markov_residuals_", each_ticker_day, support_string,"_",model_config,".rds"))
      hawkes_markov_pref_price <- hawkes
      data_file_name <- each_ticker_day
      if (model_config == "state" || model_config == "time_customizedtwo" || model_config == "state_time_customizedtwo"){
        next
      }
      pdf(paste0(save_dir, "plot_agg_nosize/hawkes_markov_pref_price_kernel_",data_file_name,support_string,model_config,".pdf"), 
          width = 5*ncol(hawkes_markov_pref_price$hawkes_kernel_est), 
          height = 5*ncol(hawkes_markov_pref_price$hawkes_kernel_est))
      plot_hawkes(hawkes_markov_pref_price)
      for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
        mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("state", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
        if (length(mu)==0){
          next
        }
        if(any(is.finite(mu))){
          barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i])
        }else{
          barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i], ylim=c(0,1))
        }
      }
      for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
        mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("time", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
        if (length(mu)==0){
          next
        }
        if(any(is.finite(mu))){
          if(model_config== model_config_list[10]){
            barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i], names.arg = time_label[2:length(time_label)])
          }else{
            barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i])
          }
        }else{
          barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i], ylim=c(0,1))
        }
      }

      dev.off()
      head(hawkes_markov_pref_price$hawkes_kernel_est)
      
      
    }else if (config_size == "withsize"){
      saveRDS(hawkes, paste0(save_dir, "data_agg_withsize/hawkes_markov_pref_price_eventsize_", each_ticker_day, support_string,"_",model_config,".rds")) 
      saveRDS(residuals, paste0(save_dir, "data_agg_withsize/hawkes_residuals_eventsize_" , each_ticker_day, support_string,"_",model_config,".rds"))
      hawkes_markov_pref_price <- hawkes
      data_file_name <- each_ticker_day
      if (model_config == "state" || model_config == "time_customizedtwo" || model_config == "state_time_customizedtwo"){
        next
      }
      pdf(paste0(save_dir, "plot_agg_withsize/hawkes_markov_pref_price_kernel_eventsize",data_file_name,support_string,model_config,".pdf"), 
          width = 5*ncol(hawkes_markov_pref_price$hawkes_kernel_est), 
          height = 5*ncol(hawkes_markov_pref_price$hawkes_kernel_est))
      plot_hawkes(hawkes_markov_pref_price)
      for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
        mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("state", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
        if (length(mu)==0){
          next
        }
        if(any(is.finite(mu))){
          barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i])
        }else{
          barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i], ylim=c(0,1))
        }
      }
      for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
        mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("time", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
        if (length(mu)==0){
          next
        }
        if(any(is.finite(mu))){
          if(model_config== model_config_list[10]){
            barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i], names.arg = time_label[2:length(time_label)])
          }else{
            barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i])
          }
        }else{
          barplot(mu, las=2, main=colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i], ylim=c(0,1))
        }
      }
      dev.off()
      head(hawkes_markov_pref_price$hawkes_kernel_est)
      
    }
    
  }
  
  
  
}



